<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html;charset=utf-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Role Index</title>
 	<%@ include file="/common/view/header.jsp"%>
 	<script type="text/javascript" src="<%=cp%>/common/js/jquery/jquery-easyui-1.3.3/datagrid-groupview.js"></script>
  	<script>
  		var win=null, form=null, grid=null;
  		var win2=null, grid2=null;
  		$(function(){
  			//create datagrid
			grid = $('#tt').datagrid({
                url:'<%=cp%>/rbac/getAllRole',
                pageSize:20,
    			pageList:[5,10,15,20],
    			loadMsg:'正在加载系统角色列表......',
    			pagination:true,
    			method:"post",
				height: document.body.clientHeight,
				rownumbers:true,
                fitColumns: true,
                singleSelect: true,
                remoteSort:false,
                idField:'id',
                columns:[[
			        {title:'角色名称',field:'comments',width:fixWidth(0.08),sortable:false,align:'left'},
                	{title:'角色代码',field:'rolename',width:fixWidth(0.16),sortable:true,align:'left'},
                	{title:'创建时间',field:'create_time',width:fixWidth(0.08),sortable:false,align:'center'},
                	{title:'最后更新时间',field:'update_time',width:fixWidth(0.08),sortable:false,align:'center'},
                	{title:'操作',field:'doaction',align:'center',width:fixWidth(0.08),align:'center',
						formatter:function(value,row,index){
							var e = '<a href="###" onclick="editDataItem('+index+')">编辑</a> ';
							var d = '<a href="###" onclick="deleteRow('+index+')">删除</a> ';
							var f = '<a href="###" onclick="shareSect('+index+')">资源分配</a>';
							return e+d+f;
						}
					}
				]],
                onDblClickRow:function(rowIndex, rowData){
                	editDataItem(rowIndex);
                },
				toolbar:[{
						id:'btnadd',
						text:'新增角色',
						iconCls:'icon-add',
						handler:newDataItem
					},'-',{
						text:'高级查询',
						iconCls:'icon-search',
						handler:openSearchForm
					},'-',{
						text:'显示全部',
						iconCls:'icon-reload',
						handler:displayAll
					},'-'
				]
            });
			grid2 = $('#sectlist_grid').treegrid({
				title:'所有资源',
    			loadMsg:'正在加载系统资源列表......',
    			method:"post",
				height: document.body.clientHeight,
				rownumbers:true,
                fitColumns: true,
                singleSelect: true,
                idField:'id',
                treeField:'comments',
                fit:true,
                columns:[[
                	{title:'选择',field:'chked',width:fixWidth(0.02),
                		formatter:function(value,row,index){
                			if(value=='1'){
                				return "<input id='"+row.id+"' type=\"checkbox\" checked==\"checked\" onclick=\"share_pro(this,'"+row.id+"');\" />";
                			}else if(value=='0'){
                				return "<input id='"+row.id+"' type=\"checkbox\" onclick=\"share_pro(this,'"+row.id+"');\" />";
                			}
                		}
                	},
			        {title:'资源名称',field:'comments',width:fixWidth(0.12),sortable:false,align:'left',
                		formatter:function(value,row,index){
                			if(row.groupid==null){
			        			return "<span style='font-weight:bold;font-size:15px'>"+value+"</span>";
			        		}else{
			        			return value;
			        		}
                		}
                	},
                	{title:'唯一URL定义',field:'sectname',width:fixWidth(0.16),sortable:false,align:'left'},
                	{title:'等级类别',field:'slevel',width:fixWidth(0.05),sortable:true,align:'center',
                		formatter:function(value,row,index){
                			return getSlevelName(value);
                		}
                	}
				]],
                onLoadSuccess:function(){
                	for(var i=1; i<4; i++){
                		$('#sectlist_grid').treegrid("mergeCells",{index:i,field:'comments',colspan:3});
                	}
                }
            });
			
			//create window
			init_window();
			init_window2();
			
			//create form
			init_form();
		});
  		/* 初始化窗口控件 */
  		function init_window(){
  			var width = 500;
  			var height = 200;
  			var top = (document.body.clientHeight-height)/2;
  			var left = (document.body.clientWidth-width)/2;
  			win = $('#data_win').window({
				closed:true,
				width: width,
				height: height,
				top: top,
  				left: left,
				modal: true,
				iconCls:'icon-app'
			});
  		}
  		function init_window2(){
  			var width = 640;
  			var height = 450;
  			var top = (document.body.clientHeight-height)/2;
  			var left = (document.body.clientWidth-width)/2;
			win2 = $('#sectlist_win').window({
				width: width,
				height: height,
				top: top,
  				left: left,
				modal: true,
				minimizable:false,
      			maximizable:false,
      			collapsible:false,
				resizable:false,
				iconCls:'icon-app'
			});
  		}
  		/* 初始化表单控件 */
  		function init_form(){
  			form = win.find('form');
  		}
  		/* 新增数据项 */
  		function newDataItem(){
  			win.window('setTitle','添加新角色');
  			win.window('open');
			form.form('clear');
			form.url = '<%=cp%>/rbac/addRole';
  		}
  		/* 编辑数据项 */
  		function editDataItem(index){
  			var row = grid.datagrid('getRows')[index];
  			if (row){
  				win.window('setTitle','更新角色');
				win.window('open');
				form.form('clear');
				form.form('load', '<%=cp%>/rbac/getRole/'+row.id+'?rnd='+Math.random());
				form.url = '<%=cp%>/rbac/editRole/'+row.id+'?rnd='+Math.random();
				
				//以下为 支持ie9需要给form中的select控件赋值
				//$('#group_id').combobox('setValue',row.groupid);
			} else {
				$.messager.show({
					title:'警告',
					msg:'请您先选择角色。'
				});
			}
  		}
  		/* 打开查询表单窗口 */
  		function openSearchForm(){
  			var width = 350;
  			var height = 100;
  			var top = (document.body.clientHeight-height)/2;
  			var left = (document.body.clientWidth-width)/2;
			$('#searchform').window({
				title: '查询',
				width: width,
				height: height,
				top: top,
  				left: left,
				modal: false,
				closed: false,
				minimizable:false,
      			maximizable:false,
      			collapsible:false,
				resizable:false,
				iconCls:'icon-search'
			});
		}
  		/* 关闭表单窗口 */
  		function closeWindow(){
			if(win)
				win.window('close');
		}
  		/* 执行查询 */
  		function do_search(){
			var name = $('#keyword').val();
			grid.datagrid('options').queryParams = {comments:name};
			grid.datagrid("options").pageNumber = 1;
			grid.datagrid('getPager').pagination({pageNumber:1});
			grid.datagrid('reload');
		}
  		/* 显示全部记录 */
  		function displayAll(){
  			grid.datagrid('load',{queryname:''});
  		}
  		/* 保存数据 */
  		function do_saveData(){
  			form.form('submit', {
				url:form.url,
				onSubmit:function(){  
					return $(this).form('validate');  
				},
				success:function(data){
					data = unescape(data);
					eval('data='+data);
					if (data.success){
						grid.datagrid('reload');
						closeWindow();
					} else {
						$.messager.alert('错误',data.msg,'error');
					}
				}
			});
  		}
  		/* 删除数据 */
  		function deleteRow(index){
			$.messager.confirm('提示','您是否确认执行删除操作？',function(r){  
				if (r){  
					var row = grid.datagrid('getRows')[index]; 
					if(row.id != ""){
						var url = "<%=cp%>/rbac/delRole/"+row.id; 
						$.post(
							url,
							function(msg) {
								$.messager.show({
									title:'提示',
									msg:'系统角色删除成功。',
									timeout:3000,
									showType:'fade'
								});
								grid.datagrid('deleteRow', index);
								grid.datagrid('reload');
							}
						);   
					}
				}  
			});  
		}
  		/* 分配资源 */
  		function shareSect(index){
  			var row = grid.datagrid('getRows')[index];
			win2.window('setTitle','为'+row.comments+'分配资源');
  			win2.window('open');
  			grid2.treegrid({url:'<%=cp%>/rbac/getAllSectForRole/'+row.id});
  		}
  		function share_pro(ochk, secid){
  			var selectedRow = grid.datagrid('getSelected');
  			if(ochk.checked){
  				//添加
  				do_share_pro("<%=cp%>/rbac/addSect2Role?rnd="+Math.random(), selectedRow.id, secid);
  			}else{
  				//删除
  				do_share_pro("<%=cp%>/rbac/delSectFromRole?rnd="+Math.random(), selectedRow.id, secid);
  			}
  		}
  		function do_share_pro(posturl, roleid, sectid){
  			$.post(posturl,{roleid:roleid, sectid:sectid},
	 			function(msg) {
					data = unescape(msg);
					eval('result='+data);
					if(result.success==1){
						$.messager.show({title:'提示',msg:result.msg,timeout:3000,showType:'fade'});
					}else{
						$.messager.alert('错误',result.msg,'error');
						$("#"+sectid).attr("checked",false);
					}
				}
	 		);
  		}
  		function changeRolename(rec){
  			switch(rec.id){
  				case "1":{$("#_rolename").text("SERV");break;}
  				case "2":{$("#_rolename").text("PJUV");break;}
  				case "3":{$("#_rolename").text("RRUV");break;}
  			}
  		}
  	</script>
  </head>
  <body style="margin:0px;" id="c_body">
  	<!-- datagrid -->
	<table id="tt"></table>
	<!-- search form -->
	<div id="searchform" class="easyui-window" closed="true" modal="true">
		<table width="100%" cellspacing="1" cellpadding="2" border="0" align="center">
		  <tr>
			<td align="center" height="60">角色名称：<input type="text" id="keyword"><a class="easyui-linkbutton" icon="icon-search" href="javascript:void(0)" onclick="do_search()">查询</a></td>
		  </tr>
		</table>
	</div>
	<!-- data item form -->
	<div id="data_win" class="easyui-window" closed="true" modal="true">  
	    <div style="text-align:center; padding:1px;">  
	        <form id="data_form" method="post">  
	            <table width="90%" cellspacing="1" cellpadding="2" border="0" align="center">
	                <tr>
	                    <td align="right">所属系统：</td>
	                    <td><select class="easyui-combobox easyui-validatebox" required="true" missingMessage="请定义所在系统" id="group_id" name="group_id" data-options="url:'<%=cp%>/rbac/getAllGroup',method:'get',textField:'title',valueField:'id',editable:false,onSelect:changeRolename" style="width:180px;"/></td>
	                </tr>
	                <tr>
	                    <td align="right">定义角色名称(中文)：</td>
	                    <td><input name="comments" class="easyui-validatebox" required="true" missingMessage="请定义并填写角色名称"></input></td>
	                </tr>
	                <tr>  
	                    <td align="right">定义角色代码(英文)：</td>
	                    <td><span id="_rolename">SERV</span>_<input name="rolename" size="12" class="easyui-validatebox" required="true" missingMessage="请定义并填写角色代码"></input></td>
	                </tr>
	            </table>
	        </form>
	    </div>
	    <div style="text-align:center;padding:5px;">
	        <a href="javascript:void(0)" onclick="do_saveData()" id="btn-save" class="easyui-linkbutton" icon="icon-save">保存</a>  
	        <a href="javascript:void(0)" onclick="closeWindow()" id="btn-cancel" class="easyui-linkbutton" icon="icon-cancel">取消</a>  
	    </div>  
	</div>
	<!-- sect list window -->
	<div id="sectlist_win" class="easyui-window" closed="true">
	  <table id="sectlist_grid"></table>
	</div>
  </body>
</html>